<template>
  <van-button
    v-if="value"
    :loading="loading"
    @click="changeFollow"
    class="follow-btn"
    round
    size="small"
  >
    已关注
  </van-button>

  <van-button
    v-else
    :loading="loading"
    @click="changeFollow"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
  >
    关注
  </van-button>
</template>
<script>
//  导入 api 请求 关注 与取消关注
import { followUser, cancleFollow } from '@/api/user.js'
export default {
  name: 'followbtn',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    aut_id: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      loading: false
    }
  },
  created() {},
  methods: {
    // 关注 与 取消关注
    async changeFollow() {
      // 点击后加载状态
      this.loading = true
      if (this.value) {
        try {
          await cancleFollow(this.aut_id)
          this.$toast.success('期待下一次遇见！')
          this.$emit('input', !this.value)
        } catch (err) {
          this.$toast.fail('出现了一些问题！亲~~')
        }
        // 不管 是否成功 都 关闭加载
        this.loading = false
      } else {
        try {
          await followUser(this.aut_id)
          this.$toast.success('感谢关注！')
          this.$emit('input', !this.value)
        } catch (err) {
          this.$toast.fail('出现了一些问题！亲~~')
        }
        // 不管 是否成功 都 关闭加载
        this.loading = false
      }
    }
  },
  computed: {},
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
</style>
